﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class BillingEntity : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSameAsAddress;
        protected HtmlForm F2051;
        protected HtmlInputHidden hdnFrom;
        protected Label lbBillingEntityID;
        protected Label lblAddress;
        protected Label lblBillAddress;
        protected Label lblBillCity;
        protected Label lblBillContact1;
        protected Label lblBillContact2;
        protected Label lblBillCountry;
        protected Label lblBillEmail1;
        protected Label lblBillEmail2;
        protected Label lblBillFax1;
        protected Label lblBillFax2;
        protected Label lblBillingEntityName;
        protected Label lblBillPhone1;
        protected Label lblBillPhone2;
        protected Label lblBillProvince;
        protected Label lblBillZip;
        protected Label lblCity;
        protected Label lblContact1;
        protected Label lblContact2;
        protected Label lblCountry;
        protected Label lblDescr;
        protected Label lblEmail1;
        protected Label lblEmail2;
        protected Label lblFax1;
        protected Label lblFax2;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblPhone1;
        protected Label lblPhone2;
        protected Label lblProvince;
        protected Label lblRemark;
        protected Label lblUserDefine1;
        protected Label lblUserDefine10;
        protected Label lblUserDefine2;
        protected Label lblUserDefine3;
        protected Label lblUserDefine4;
        protected Label lblUserDefine5;
        protected Label lblUserDefine6;
        protected Label lblUserDefine7;
        protected Label lblUserDefine8;
        protected Label lblUserDefine9;
        protected Label lblVAT;
        protected Label lblZip;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Label one4;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAddress;
        protected TextBox txtBillAddress;
        protected TextBox txtBillCity;
        protected TextBox txtBillConatact2;
        protected TextBox txtBillContact1;
        protected TextBox txtBillCountry;
        protected TextBox txtBillEmail1;
        protected TextBox txtBillEmail2;
        protected TextBox txtBillFax1;
        protected TextBox txtBillFax2;
        protected TextBox txtBillingEntityID;
        protected TextBox txtBillingEntityName;
        protected TextBox txtBillPhone1;
        protected TextBox txtBillPhone2;
        protected TextBox txtBillProvince;
        protected TextBox txtBillZIP;
        protected TextBox txtCity;
        protected TextBox txtConatact2;
        protected TextBox txtContact1;
        protected TextBox txtCountry;
        protected TextBox txtEmail1;
        protected TextBox txtEmail2;
        protected TextBox txtFax1;
        protected TextBox txtFax2;
        protected TextBox txtPhone1;
        protected TextBox txtPhone2;
        protected TextBox txtProvince;
        protected TextBox txtRemark;
        protected TextBox txtUserDefine1;
        protected TextBox txtUserDefine10;
        protected TextBox txtUserDefine2;
        protected TextBox txtUserDefine3;
        protected TextBox txtUserDefine4;
        protected TextBox txtUserDefine5;
        protected TextBox txtUserDefine6;
        protected TextBox txtUserDefine7;
        protected TextBox txtUserDefine8;
        protected TextBox txtUserDefine9;
        protected TextBox txtVAT;
        protected TextBox txtVendorDescr;
        protected TextBox txtZIP;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtBillingEntityID.Text = "";
            this.txtBillingEntityID.Enabled = true;
            this.txtBillingEntityName.Text = "";
            this.txtBillingEntityName.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtBillingEntityID.Text.Trim()) && !string.IsNullOrEmpty(this.txtBillingEntityName.Text.Trim()))
            {
                try
                {
                    List<string> BillingEntityID = new List<string> {
                        this.txtBillingEntityID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(BillingEntityID, "BILLING_ENTITY") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("BillingEntityQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("BillingEntityList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtBillingEntityID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2150_001");
            }
            else if (string.IsNullOrEmpty(this.txtBillingEntityName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2150_002");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateBillingEntityInfo();
            }
            else
            {
                this.InsertBillingEntityInfo();
            }
        }

        protected void chkSameAsAddress_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSameAsAddress.Checked)
            {
                this.txtBillCity.Text = this.txtCity.Text;
                this.txtBillProvince.Text = this.txtProvince.Text;
                this.txtBillContact1.Text = this.txtContact1.Text;
                this.txtBillConatact2.Text = this.txtConatact2.Text;
                this.txtBillCountry.Text = this.txtCountry.Text;
                this.txtBillZIP.Text = this.txtZIP.Text;
                this.txtBillPhone1.Text = this.txtPhone1.Text;
                this.txtBillPhone2.Text = this.txtPhone2.Text;
                this.txtBillAddress.Text = this.txtAddress.Text;
                this.txtBillFax1.Text = this.txtFax1.Text;
                this.txtBillFax2.Text = this.txtFax2.Text;
                this.txtBillEmail1.Text = this.txtEmail1.Text;
                this.txtBillEmail2.Text = this.txtEmail2.Text;
                ScriptManager.RegisterStartupScript((Page) this, base.GetType(), "", "<script language='JavaScript'>setTab('one',3,4);</script>", false);
            }
        }

        private void InsertBillingEntityInfo()
        {
            try
            {
                BillingEntityInfo billingEntityInfo = new BillingEntityInfo {
                    BillingEntityID = this.txtBillingEntityID.Text.Trim(),
                    Company = this.txtBillingEntityName.Text.Trim(),
                    Description = this.txtVendorDescr.Text.Trim(),
                    Vat = this.txtVAT.Text.Trim(),
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID,
                    City = this.txtCity.Text.Trim(),
                    State = this.txtProvince.Text.Trim(),
                    Contact1 = this.txtContact1.Text.Trim(),
                    Contact2 = this.txtConatact2.Text.Trim(),
                    Country = this.txtCountry.Text.Trim(),
                    Zip = this.txtZIP.Text.Trim(),
                    Phone1 = this.txtPhone1.Text.Trim(),
                    Phone2 = this.txtPhone2.Text.Trim(),
                    Address = this.txtAddress.Text.Trim(),
                    Fax1 = this.txtFax1.Text.Trim(),
                    Fax2 = this.txtFax2.Text.Trim(),
                    Email1 = this.txtEmail1.Text.Trim(),
                    Email2 = this.txtEmail2.Text.Trim()
                };
                if (this.chkSameAsAddress.Checked)
                {
                    billingEntityInfo.BCity = billingEntityInfo.City;
                    billingEntityInfo.BState = billingEntityInfo.State;
                    billingEntityInfo.BContact1 = billingEntityInfo.Contact1;
                    billingEntityInfo.BContact2 = billingEntityInfo.Contact2;
                    billingEntityInfo.BCountry = billingEntityInfo.Country;
                    billingEntityInfo.BZip = billingEntityInfo.Zip;
                    billingEntityInfo.BPhone1 = billingEntityInfo.Phone1;
                    billingEntityInfo.BPhone2 = billingEntityInfo.Phone2;
                    billingEntityInfo.BAddress = billingEntityInfo.Address;
                    billingEntityInfo.BFax1 = billingEntityInfo.Fax1;
                    billingEntityInfo.BFax2 = billingEntityInfo.Fax2;
                    billingEntityInfo.BEmail1 = billingEntityInfo.Email1;
                    billingEntityInfo.BEmail2 = billingEntityInfo.Email2;
                }
                else
                {
                    billingEntityInfo.BCity = this.txtBillCity.Text.Trim();
                    billingEntityInfo.BState = this.txtProvince.Text.Trim();
                    billingEntityInfo.BContact1 = this.txtBillContact1.Text.Trim();
                    billingEntityInfo.BContact2 = this.txtBillConatact2.Text.Trim();
                    billingEntityInfo.BCountry = this.txtBillCountry.Text.Trim();
                    billingEntityInfo.BZip = this.txtBillZIP.Text.Trim();
                    billingEntityInfo.BPhone1 = this.txtBillPhone1.Text.Trim();
                    billingEntityInfo.BPhone2 = this.txtBillPhone2.Text.Trim();
                    billingEntityInfo.BAddress = this.txtBillAddress.Text.Trim();
                    billingEntityInfo.BFax1 = this.txtBillFax1.Text.Trim();
                    billingEntityInfo.BFax2 = this.txtBillFax2.Text.Trim();
                    billingEntityInfo.BEmail1 = this.txtBillEmail1.Text.Trim();
                    billingEntityInfo.BEmail2 = this.txtBillEmail2.Text.Trim();
                }
                billingEntityInfo.Udf1 = this.txtUserDefine1.Text.Trim();
                billingEntityInfo.Udf2 = this.txtUserDefine2.Text.Trim();
                billingEntityInfo.Udf3 = this.txtUserDefine3.Text.Trim();
                billingEntityInfo.Udf4 = this.txtUserDefine4.Text.Trim();
                billingEntityInfo.Udf5 = this.txtUserDefine5.Text.Trim();
                billingEntityInfo.Udf6 = this.txtUserDefine6.Text.Trim();
                billingEntityInfo.Udf7 = this.txtUserDefine7.Text.Trim();
                billingEntityInfo.Udf8 = this.txtUserDefine8.Text.Trim();
                billingEntityInfo.Udf9 = this.txtUserDefine9.Text.Trim();
                billingEntityInfo.Udf10 = this.txtUserDefine10.Text.Trim();
                billingEntityInfo.Remark = this.txtRemark.Text.Trim();
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(billingEntityInfo, "BILLING_ENTITY") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = billingEntityInfo;
                this.txtBillingEntityID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetBillingEntityInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetBillingEntityInfo2Form()
        {
            try
            {
                string CustomerID = base.Request.QueryString["ID"].ToString();
                BillingEntityInfo billingEntityInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(CustomerID, "BILLING_ENTITY") as BillingEntityInfo;
                if (billingEntityInfo != null)
                {
                    base.DataEntityViewState = billingEntityInfo;
                    this.txtBillingEntityID.Text = billingEntityInfo.BillingEntityID;
                    this.txtBillingEntityName.Text = billingEntityInfo.Company;
                    this.txtVendorDescr.Text = billingEntityInfo.Description;
                    this.txtVAT.Text = billingEntityInfo.Vat;
                    this.chkIsActive.Checked = billingEntityInfo.IsActive == "Y";
                    this.txtCity.Text = billingEntityInfo.City;
                    this.txtProvince.Text = billingEntityInfo.State;
                    this.txtContact1.Text = billingEntityInfo.Contact1;
                    this.txtConatact2.Text = billingEntityInfo.Contact2;
                    this.txtCountry.Text = billingEntityInfo.Country;
                    this.txtZIP.Text = billingEntityInfo.Zip;
                    this.txtPhone1.Text = billingEntityInfo.Phone1;
                    this.txtPhone2.Text = billingEntityInfo.Phone2;
                    this.txtAddress.Text = billingEntityInfo.Address;
                    this.txtFax1.Text = billingEntityInfo.Fax1;
                    this.txtFax2.Text = billingEntityInfo.Fax2;
                    this.txtEmail1.Text = billingEntityInfo.Email1;
                    this.txtEmail2.Text = billingEntityInfo.Email2;
                    this.txtBillCity.Text = billingEntityInfo.BCity;
                    this.txtBillProvince.Text = billingEntityInfo.BState;
                    this.txtBillContact1.Text = billingEntityInfo.BContact1;
                    this.txtBillConatact2.Text = billingEntityInfo.BContact2;
                    this.txtBillCountry.Text = billingEntityInfo.BCountry;
                    this.txtBillZIP.Text = billingEntityInfo.BZip;
                    this.txtBillPhone1.Text = billingEntityInfo.BPhone1;
                    this.txtBillPhone2.Text = billingEntityInfo.BPhone2;
                    this.txtBillAddress.Text = billingEntityInfo.BAddress;
                    this.txtBillFax1.Text = billingEntityInfo.BFax1;
                    this.txtBillFax2.Text = billingEntityInfo.BFax2;
                    this.txtBillEmail1.Text = billingEntityInfo.BEmail1;
                    this.txtBillEmail2.Text = billingEntityInfo.BEmail2;
                    this.txtUserDefine1.Text = billingEntityInfo.Udf1;
                    this.txtUserDefine2.Text = billingEntityInfo.Udf2;
                    this.txtUserDefine3.Text = billingEntityInfo.Udf3;
                    this.txtUserDefine4.Text = billingEntityInfo.Udf4;
                    this.txtUserDefine5.Text = billingEntityInfo.Udf5;
                    this.txtUserDefine6.Text = billingEntityInfo.Udf6;
                    this.txtUserDefine7.Text = billingEntityInfo.Udf7;
                    this.txtUserDefine8.Text = billingEntityInfo.Udf8;
                    this.txtUserDefine9.Text = billingEntityInfo.Udf9;
                    this.txtUserDefine10.Text = billingEntityInfo.Udf10;
                    this.txtRemark.Text = billingEntityInfo.Remark;
                    this.txtBillingEntityID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateBillingEntityInfo()
        {
            try
            {
                BillingEntityInfo billingEntityInfo = base.DataEntityViewState as BillingEntityInfo;
                billingEntityInfo.Company = this.txtBillingEntityName.Text.Trim();
                billingEntityInfo.Description = this.txtVendorDescr.Text.Trim();
                billingEntityInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                billingEntityInfo.UpdatedBy = base.UserInfo.UserID;
                billingEntityInfo.City = this.txtCity.Text.Trim();
                billingEntityInfo.State = this.txtProvince.Text.Trim();
                billingEntityInfo.Contact1 = this.txtContact1.Text.Trim();
                billingEntityInfo.Contact2 = this.txtConatact2.Text.Trim();
                billingEntityInfo.Country = this.txtCountry.Text.Trim();
                billingEntityInfo.Zip = this.txtZIP.Text.Trim();
                billingEntityInfo.Phone1 = this.txtPhone1.Text.Trim();
                billingEntityInfo.Phone2 = this.txtPhone2.Text.Trim();
                billingEntityInfo.Address = this.txtAddress.Text.Trim();
                billingEntityInfo.Fax1 = this.txtFax1.Text.Trim();
                billingEntityInfo.Fax2 = this.txtFax2.Text.Trim();
                billingEntityInfo.Email1 = this.txtEmail1.Text.Trim();
                billingEntityInfo.Email2 = this.txtEmail2.Text.Trim();
                if (this.chkSameAsAddress.Checked)
                {
                    billingEntityInfo.BCity = billingEntityInfo.City;
                    billingEntityInfo.BState = billingEntityInfo.State;
                    billingEntityInfo.BContact1 = billingEntityInfo.Contact1;
                    billingEntityInfo.BContact2 = billingEntityInfo.Contact2;
                    billingEntityInfo.BCountry = billingEntityInfo.Country;
                    billingEntityInfo.BZip = billingEntityInfo.Zip;
                    billingEntityInfo.BPhone1 = billingEntityInfo.Phone1;
                    billingEntityInfo.BPhone2 = billingEntityInfo.Phone2;
                    billingEntityInfo.BAddress = billingEntityInfo.Address;
                    billingEntityInfo.BFax1 = billingEntityInfo.Fax1;
                    billingEntityInfo.BFax2 = billingEntityInfo.Fax2;
                    billingEntityInfo.BEmail1 = billingEntityInfo.Email1;
                    billingEntityInfo.BEmail2 = billingEntityInfo.BEmail2;
                }
                else
                {
                    billingEntityInfo.BCity = this.txtBillCity.Text.Trim();
                    billingEntityInfo.BState = this.txtBillProvince.Text.Trim();
                    billingEntityInfo.BContact1 = this.txtBillContact1.Text.Trim();
                    billingEntityInfo.BContact2 = this.txtBillConatact2.Text.Trim();
                    billingEntityInfo.BCountry = this.txtBillCountry.Text.Trim();
                    billingEntityInfo.BZip = this.txtBillZIP.Text.Trim();
                    billingEntityInfo.BPhone1 = this.txtBillPhone1.Text.Trim();
                    billingEntityInfo.BPhone2 = this.txtBillPhone2.Text.Trim();
                    billingEntityInfo.BAddress = this.txtBillAddress.Text.Trim();
                    billingEntityInfo.BFax1 = this.txtBillFax1.Text.Trim();
                    billingEntityInfo.BFax2 = this.txtBillFax2.Text.Trim();
                    billingEntityInfo.BEmail1 = this.txtBillEmail1.Text.Trim();
                    billingEntityInfo.BEmail2 = this.txtBillEmail2.Text.Trim();
                }
                billingEntityInfo.Udf1 = this.txtUserDefine1.Text.Trim();
                billingEntityInfo.Udf2 = this.txtUserDefine2.Text.Trim();
                billingEntityInfo.Udf3 = this.txtUserDefine3.Text.Trim();
                billingEntityInfo.Udf4 = this.txtUserDefine4.Text.Trim();
                billingEntityInfo.Udf5 = this.txtUserDefine5.Text.Trim();
                billingEntityInfo.Udf6 = this.txtUserDefine6.Text.Trim();
                billingEntityInfo.Udf7 = this.txtUserDefine7.Text.Trim();
                billingEntityInfo.Udf8 = this.txtUserDefine8.Text.Trim();
                billingEntityInfo.Udf9 = this.txtUserDefine9.Text.Trim();
                billingEntityInfo.Udf10 = this.txtUserDefine10.Text.Trim();
                billingEntityInfo.Remark = this.txtRemark.Text.Trim();
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(billingEntityInfo, "BILLING_ENTITY") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

